package com.ruoyi.system.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;

public class SysDateUtils {

	/**
	 * 获得指定时间格式的Date时间
	 * @author wangzhe@piesat.cn
	 * @date 2018年7月21日
	 * @param time 时间字符串
	 * @param plan 时间格式
	 * @return 时间
	 */
	public static Date getDateByString(String time,String plan){
		Long times = getLongTimeByString(time, plan);
		return  new Date(times);
	}

	/**
	 * 获得指定时间格式的时间
	 * @author wangzhe@piesat.cn
	 * @date 2018年7月21日
	 * @param time 时间字符串
	 * @param plan 时间格式
	 * @return 时间
	 */
	public static String getStringDateByString(Date time,String plan){
		DateFormat format = new SimpleDateFormat(plan);
		return format.format(time);
	}

	/**
	 * 获得指定时间格式的毫秒值
	 * @author wangzhe@piesat.cn
	 * @date 2018年7月17日
	 * @param time 时间字符串
	 * @param plan 时间格式
	 * @return 毫秒值
	 */
	public static Long getLongTimeByString(String time,String plan){
		DateFormat format = new SimpleDateFormat(plan);
		Long result = null;
		try {
			result = format.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return  result;
	}
	
	public static int getCourseWeekNum(Date start, Date end){
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setMinimalDaysInFirstWeek(7);
		cal.setTime(start);
		
		int startNum = cal.get(Calendar.WEEK_OF_YEAR);
		int endNum = 0;
		cal.setTime(end);
		endNum = cal.get(Calendar.WEEK_OF_YEAR);
		if(endNum<startNum){
			cal.setTime(start);
			int year = cal.get(Calendar.YEAR);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			try {
				Date lastday = sdf.parse(year+"1230");
				cal.setTime(lastday);
				endNum += cal.get(Calendar.WEEK_OF_YEAR);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return endNum - startNum +1;
	}
	
	public static List<String> getCourseWeekNumDate(Date start, int weekNum){
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setMinimalDaysInFirstWeek(7);
		cal.setTime(start);
		cal.add(Calendar.DAY_OF_YEAR, 7*(weekNum - 1));
		List<String> weekDayList = new ArrayList<String>();
		weekDayList.add(DateUtils.parseDateToStr("yyyy-MM-dd", cal.getTime()));
		for(int i=1; i<7; i++){
			cal.add(Calendar.DAY_OF_YEAR, 1);
			weekDayList.add(DateUtils.parseDateToStr("yyyy-MM-dd", cal.getTime()));
		}
		return weekDayList;
	}
	
   public static List<Date> getCourseDates(Date startDate, Date endDate, int weekDay) {
		List<Date> dates = new ArrayList<>();
		Calendar instance = Calendar.getInstance();
		instance.setTime(startDate);
		while (instance.getTime().compareTo(endDate) <= 0) {
			int dayOfWeek = instance.get(Calendar.DAY_OF_WEEK);
			if (dayOfWeek == weekDay) {
				dates.add(instance.getTime());
			}
			instance.add(Calendar.DAY_OF_YEAR, 1);
		}
		return dates;
	}
	
	public static void main(String[] args) throws ParseException{
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date startDate = sdf.parse("20221023");
		Calendar instance = Calendar.getInstance();
		instance.setTime(startDate);
		int dayOfWeek = instance.get(Calendar.DAY_OF_WEEK);*/
		//Date endDate = sdf.parse("20230108");
		//Date courseStartDate = DateUtils.addDays(startDate, 7*6 );
		//Date courseEndDate = DateUtils.addDays(startDate, 7*15 );
		//List<Date> dates = getCourseDates(courseStartDate,courseEndDate, Calendar.MONDAY);
		//System.out.println(dayOfWeek);
		//System.out.println(getCourseWeekNum(sdf.parse("20220829"),sdf.parse("20221028")));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date startDate = sdf.parse("20220829");
		List<String> weekDaylist = getCourseWeekNumDate(startDate,19);
		weekDaylist.forEach(item->{
			System.out.println(item);
		});
	}
	
}

